<?php
class CommonAction extends BaseAction
{
    public $_role = null;
    public $_mall = null;
    public $_mid = null;
    public $_fid = null;
    public $_bids = null;
    public $_level = null;

    /**
     * 默认初始化
     *
     * @author ice<iceinto@mallog.com.cn>
     */
    function _initialize()
    {
        parent::_initialize();
        // 用户权限检查
        if ($this->getLoginUserInfo()) {
            $uid = $this->getLoginUserInfo();
            $map = array();
            $map ['user_id'] = $uid;
            $role = M('RoleUser')->where($map)->find();
            $this->_role = $role;
            if ($role) {
                $this->setOnline($uid); //Set Online
                if ($role ['role_id'] == 5) {
                    //默认编辑可以访问所有
                } else if (GROUP_NAME != 'Home' && GROUP_NAME != 'Robot' && $role ['role_id'] == 4) {
                    redirect('/');
                } else if ((GROUP_NAME == 'Home' && GROUP_NAME != 'Robot' && $role ['role_id'] != 4)) {
                    redirect(__APP__ . '/Task');
                }
                if ($role ['role_id'] == 4) {
                    $this->setUserMall($uid);
                }
            } else {
                $this->errorLang('_NOT_ACCESS');
            }
            // 登陆的用户
            $user = D('User')->getOne($uid);
            $this->assign('login_user', $user);
            $this->assign('login_role', $role);
        } else {
            // 需要登陆
            $this->assign('jumpUrl', PHP_FILE . C('USER_AUTH_GATEWAY'));
            $this->error(L('_MUST_LOGIN'));
        }
    }

    /**
     * 获取单个用户信息
     *
     * @param int $uid
     * @author ice<iceinto@mallog.com.cn>
     */
    function getUserById($uid)
    {
        if (empty($uid)) {
            return false;
        }
        return D('User')->getOne($uid);
    }

    public function setUserMall($uid)
    {
        $this->_mall = D('Per')->getUserMall($uid);
        if ($this->_mall) {
            $mid = $this->_mall[0]['mid'];
            $this->_mid = $mid;
            $this->_fid = $this->_mall[0]['fid'];
            $this->_bids = explode(',', $this->_mall[0]['bids']);
            $this->_level = $this->_mall[0]['level'];
            $this->assign('user_bids', $this->_bids);
            $this->assign('user_fid', $this->_fid);
            $this->assign('user_level', $this->_level);
            $this->assign('user_mid', $mid);
        } else {
            unset($_SESSION);
            Session::destroy();
            Cookie::clear();
            $this->error('你还没有权限登入，进对你进行强制退出');
        }
    }

    public function getMallId()
    {
        return $this->_mid;
    }

    /**
     * Access to current task system information
     *
     * @author ice<iceinto@mallog.com.cn>
     */
    public function getTaskSituation()
    {
        $QueueModel = D('Queue');
        $map = array();

        $map ['status'] = array('IN', '1,2');
        $TaskModel = M('Task');
        $allCount = $TaskModel->count();

        unset($map);
        $map ['status'] = 1;
        $waitingCount = $TaskModel->where($map)->count();
        $receivingCount = $QueueModel->getStatusCount(2);
        $inCount = $QueueModel->getStatusCount(3);
        $returnCount = $QueueModel->getStatusCount(4);
        $returnOKCount = $QueueModel->getStatusCount(5);
        $waitingAuditCount = $QueueModel->getStatusCount(6);
        $auditReturnCount = $QueueModel->getStatusCount(7);
        $auditReturnOKCount = $QueueModel->getStatusCount(8);
        $auditCount = $QueueModel->getStatusCount(9);
        $waitingQueueExt = $QueueModel->getStatusCount(70);
        $allExt = M('ActivityExtension')->count();
        $waitingExt = M('ActivityExtension')->where('status=1')->count();

        $this->assign('onlineCount', $this->getOnlineUserCount());
        $this->assign('allCount', $allCount);
        $this->assign('waitingCount', $waitingCount);
        $this->assign('inCount', $inCount);
        $this->assign('returnCount', $returnCount);
        $this->assign('returnOKCount', $returnOKCount);
        $this->assign('waitingAuditCount', $waitingAuditCount);
        $this->assign('auditReturnCount', $auditReturnCount);
        $this->assign('auditReturnOKCount', $auditReturnOKCount);
        $this->assign('auditCount', $auditCount);
        $this->assign('receivingCount', $receivingCount);
        $this->assign('allExt', $allExt);
        $this->assign('waitingExt', $waitingExt);
        $this->assign('waitingQueueExt', $waitingQueueExt);
    }

    /**
     * 获取授权管理
     * @param int $mid
     * @param int $fid
     * @param int $mid
     */
    public function getMallAuthorize($mid, $fid = null, $bid = null)
    {
        $auth = false;
        $level = $this->_level;
        if ($this->_role['role_id'] == 5) {
            return true;
        }
        if ($level == 1) {
            if ($mid == $this->_mid) {
                $auth = true;
            }
        } else if ($level == 2) {
            if ($mid == $this->_mid && $fid == $this->_fid) {
                $auth = true;
            }
        } else if ($level == 3) {
            if ($mid == $this->_mid && $fid == $this->_fid && in_array($bid, $this->_bids)) {
                $auth = true;
            }
        }
        return $auth;
    }

    public function ajaxIsAuthorize($mid, $fid = null, $bid = null)
    {
        $auth = $this->getMallAuthorize($mid, $fid, $bid);
        if (!$auth) {
            $this->ajaxReturn(L('_VALID_ACCESS_'), null, 0);
        }
    }

    /**
     * 获取年
     * @param int $month
     */
    public function getYears($year = 0)
    {
        $year = 2012;
        $years = array();
        for ($i = date('Y'); $i >= $year; $i--) {
            $years[] = array('year' => $i, 'isNow' => ($i == $year));
        }
        return $years;
    }

    /**
     * 获取月份
     * @param int $month
     */
    public function getMonths($month = 0)
    {
        if (empty($month)) {
            $month = date('n'); // n 数字表示的月份，没有前导零1 到 12
        }
        $months = array();
        for ($i = 1; $i <= 12; $i++) {
            $months[] = array('month' => $i, 'isNow' => ($i == $month));
        }
        return $months;
    }
}

?>